// 临时页面跳转功能
$(document).on("click","[data-url]",function(){
    var url = $(this).data("url");
    if(url == "back"){
        history.back(-1);
    }
    else{
        $(location).attr('href', url);    
    }
});


// 日期
var datelist = {
	beforeyesterday: moment(moment().subtract(2, 'days')).format("YYYY-MM-DD"),
	yesterday: moment(moment().subtract(1, 'days')).format("YYYY-MM-DD"),
	today: moment().format("YYYY-MM-DD"),
	tomorrow: moment(moment().add(1, 'days')).format("YYYY-MM-DD"),
	aftertomorrow: moment(moment().add(2, 'days')).format("YYYY-MM-DD"),
	// 日期转中文
	dateToText: function(date,other,time){
		var date2 = moment(date).format("YYYY-MM-DD");
        var text = moment(date).format("M月D日");
        if(other){
        	if(date2 == datelist.beforeyesterday) text +=" 前天";
	        if(date2 == datelist.yesterday) text +=" 昨天";
	        if(date2 == datelist.today) text +=" 今天";
	        if(date2 == datelist.tomorrow) text +=" 明天";
	        if(date2 == datelist.aftertomorrow) text +=" 后天";	
        }
        if(time){
        	text += moment(date).format(" H时m分")
        }
        return text
    }
}


// 小提示框
var tip = {
	num: 0,
	// 插入提示框
	show: function(text,time){
		this.num ++;
		var index = this.num;
		var time = time ? time : 3000;
		$("body").append('<div class="tip-box" index="'+index+'">'+text+'</div>');
		var $el = $(".tip-box[index="+ index +"]");
		setTimeout(function(){
            $el.addClass("hide");
            setTimeout(function(){
        		$el.remove();
            },500);
        },time);
	}
};


// 对话框
var dialog = {
	show: function(id,title,text,btn1,fun1,btn2,fun2){
		$("body").append(
			'<div class="dialog" id="'+ id +'">'+
		        '<div class="dialog-box">'+
		            '<div class="dialog-body">'+
		                '<h2>'+ title +'</h2>'+
		                '<p>'+
		                    text+
		                '</p>'+
		            '</div>'+
		            '<div class="dialog-bottom">'+
		                '<button class="btn middle-btn gray-btn" name="btn2">'+ btn2 +'</button>'+
		                '<button class="btn middle-btn main-btn" name="btn1">'+ btn1 +'</button>'+
		            '</div>'+
		        '</div>'+
		    '</div>'
		);
		$("#"+id+" [name=btn1]").bind("click",function(){
			fun1();
		});
		$("#"+id+" [name=btn2]").bind("click",function(){
			fun2();
		});
	},
	hide: function(id){
		$("#"+id).remove();
	}
};


// 关闭页面
function closeWeb(){
	window.close();
	WeixinJSBridge.call('closeWindow');
}